package com.example.demospring.controller;

import com.example.demospring.business.entity.ImgSave;
import com.example.demospring.business.service.ImgsServer;
import com.example.demospring.common.entity.Message;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.example.demospring.common.image.ImageDao ;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/img")
public class ImgsController {
    private ImageDao imageDao;

    private final ImgsServer imgsServer;

    @Autowired
    public ImgsController(ImageDao imageDao, ImgsServer imgsServer) {
        this.imageDao = imageDao;
        this.imgsServer = imgsServer;
    }
    // 查询整张表
    @GetMapping("/all")
    public List<ImgSave> showAllImgs() {
        return imgsServer.showImgs();
    }

    @GetMapping("/date")
    public List<ImgSave> showImgsByDate(@Param("date") String date) {
        return imgsServer.showImgsByDate(date);
    }

    @PostMapping("/upload")
    public Message upload(@RequestParam("file") MultipartFile file){
        if(file.isEmpty()){
            return Message.fail("图片传输失败");
        }
        return imageDao.upload(file);
    }

    @GetMapping("/delImg")
    public Message delImg(@Param("md5") String md5){
        return imageDao.delete(md5);
    }
    @PutMapping("/addImg")
    public Message addImg(@RequestBody ImgSave imgSave){
        if(imgSave.getImgRemarks().isEmpty()){
            return Message.fail("请写备注");
        }
        if(imgSave.getImgUrl().length() != 32){
            return Message.fail("url传输错误");
        }
        return imgsServer.addImg(imgSave);
    }
}
